package com.huawei;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.huawei.entity.Users;
import com.huawei.mapper.UsersMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;
import java.util.Scanner;

@SpringBootTest
public class SecurityEncode {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    static Scanner lyq = new Scanner(System.in);

    @Test
    public void encode() {
        //对数据库用户表中的所有密码进行加密操作

        List<Users> users = usersMapper.selectList(null);

        for(Users user:users){
            String encodePassword = passwordEncoder.encode(user.getPassword());

            LambdaUpdateWrapper<Users> luw=new LambdaUpdateWrapper<>();
            luw.eq(Users::getId,user.getId())
                .set(Users::getPassword, encodePassword);
            int update = usersMapper.update(user, luw);
            if(update>0)
                System.out.println("用户【"+user.getUsername()+"】的密码已加密成功喵~");
            else
                System.out.println("用户【"+user.getUsername()+"】的密码加密失败了喵~");
        };
    }

    @Test
    public void verifyEncodePassword(){
        List<Users> users = usersMapper.selectList(null);
        System.out.println("用户数量："+users.size());
        for (Users user : users) {
            String userPasswordEncode = user.getPassword();//获取密文密码
            System.out.println("请输入要验证的密码 【当前用户: "+user.getUsername()+"】 ");
            String password = lyq.next();
            if(passwordEncoder.matches(password,userPasswordEncode))
                System.out.println("验证成功喵~");
            else System.out.println("验证失败喵~");
        };
    }

    @Test
    public void testInput(){
        System.out.println("请输入内容： ");
        Scanner in = new Scanner(System.in);
        String content = in.next();
        System.out.println("输入的内容是： "+content);
    }
}
